package com.ht.module.ai.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.constant.AiChatMessageRoleEnum;
import com.ht.module.ai.entity.AiChatMessage;
import com.ht.module.ai.entity.AiChatSession;
import com.ht.module.ai.mapper.AiChatMessageMapper;
import com.ht.module.ai.mapper.AiChatSessionMapper;
import com.ht.module.ai.service.AiChatSessionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.module.ai.vo.AiChatVo;
import com.ht.module.sys.entity.SysUser;
import com.ht.util.CommMethod;
import com.ht.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * AI聊天会话表 服务实现类
 * </p>
 *
 * @author hejialun
 * @since 2025-06-09
 */
@Slf4j
@Service
public class AiChatSessionServiceImpl extends ServiceImpl<AiChatSessionMapper, AiChatSession> implements AiChatSessionService {


    @Resource
    private AiChatMessageMapper aiChatMessageMapper;


    @Override
    public List<AiChatSession> findListByUser(AiChatSession en) {
        QueryWrapper<AiChatSession> qw = getQW(en);
        qw.eq("create_user", UserUtil.getUserId());
        qw.orderByDesc("update_date");
        return baseMapper.selectList(qw);
    }


    private QueryWrapper<AiChatSession> getQW(AiChatSession en) {
        QueryWrapper<AiChatSession> qw = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(en.getTitle())) {
            qw.eq("title", en.getTitle());
        }
        return qw;
    }

    /**
     * 创建会话
     *
     * @param msg
     */
    @Override
    @Transactional
    public AiChatSession createSession(String sessionId, String msg) {
        AiChatSession session = new AiChatSession();
        CommMethod.beanCreate(session);
        session.setId(sessionId)
                .setLastMessageContent(msg)
                .setTitle(msg.length() > 100 ? msg.substring(0, 100) : msg)
                .setLastMessageTime(session.getCreateDate())
                .setLastMessageContent(msg)
                .setMemoryContext(msg);
        //保存session
        baseMapper.insert(session);


      /*  //插入一个回复信息
        AiChatMessage message = new AiChatMessage()
                .setMessageTime(session.getCreateDate())
                .setUserId(UserUtil.getUserId())
                .setSessionId(session.getId())
                .setRole(AiChatMessageRoleEnum.USER.getCode())
                .setContent(msg);

        CommMethod.beanCreate(message);

        //保存消息对话
        aiChatMessageMapper.insert(message);*/

        return session;
    }


    /**
     * 添加回话消息
     *
     * @param vo
     */
    @Override
    @Transactional
    public AiChatSession addSessionMessage(AiChatVo vo, AiChatMessageRoleEnum role) {
        //查询出当前回话
        AiChatSession session = baseMapper.selectById(vo.getSessionId());
        session.setLastMessageContent(vo.getMsg())
                .setLastMessageTime(session.getCreateDate())
                .setLastMessageContent(vo.getMsg())
                .setMemoryContext(session.getMemoryContext() + "," + vo.getMsg());
        CommMethod.beanUpdate(session);
        //修改session
        baseMapper.updateById(session);

        //插入一个回复信息
        AiChatMessage message = new AiChatMessage()
                .setMessageTime(session.getUpdateDate())
                .setSessionId(session.getId())
                .setRole(role.getCode())
                .setContent(vo.getMsg());

        CommMethod.beanCreate(message);

        //保存消息对话
        aiChatMessageMapper.insert(message);

        return session;
    }
}
